home *** CD-ROM | disk | FTP | other *** search
Java Source | 1995-12-31 | 2.4 KB | 128 lines |
- import java.net.*;
- import java.io.*;
- import java.applet.*;
- import java.awt.*;
- import java.util.Random;
-
- /* Network Client */
- /* YOU NEED TO REPLACE THE SERVER STRING WITH YOUR MACHINE (BELOW) */
-
- public class Netc extends Applet implements Runnable {
-
- Socket sock;
- DataInputStream datain;
- DataOutputStream dataout;
- Thread mythread = null;
- Point All[] = new Point[10];
- Point pos = new Point(254,254);
- int num;
-
-
- /* Applet initialization */
- public void init() {
-
- resize(250,250);
-
- for (int g=0;g<10;++g) All[g] = new Point(-1,-1);
- }
-
- /* Applet start */
- public void start() {
-
-
- /* Open socket to the server and setup the streams */
- try {
- InetAddress addr = InetAddress.getByName("serval.cat.syr.edu");
- sock = new Socket(addr,1237);
- datain = new
- DataInputStream(new BufferedInputStream(sock.getInputStream()));
- dataout = new
- DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
- } catch (IOException E) {}
-
-
- if (mythread == null) {
- mythread = new Thread(this);
- mythread.start();
- }
- }
-
- /* Applet stop */
- public void stop() {
- if (mythread != null) {
- mythread.stop();
- mythread=null;
-
- /* additional cleanup (closings) */
- try {
- dataout.close();
- datain.close();
- sock.close();
- } catch (IOException E);
- }
- }
-
-
- /* The Thread's run method */
- public void run() {
- int tx,ty;
-
- while (true) {
- try {
- synchronized (pos) {
- dataout.write(pos.x);
- dataout.write(pos.y);
- pos.x = pos.y = 0;
- }
- dataout.flush();
- tx = num = 0;
-
- do {
- tx = datain.read();
- ty = datain.read();
- All[num].move(tx,ty);
- num++;
- } while (tx != 255);
-
- } catch (IOException e) {}
- catch (NullPointerException e) {
- throw new NullPointerException("ACK!");
- }
-
- repaint();
- mythread.yield();
- }
- }
-
- public boolean mouseDrag(Event E, int x, int y) {
- synchronized(pos) {
- pos.x = x;
- pos.y = y;
- }
- return true;
- }
- public boolean mouseDown(Event E, int x, int y) {
- mouseDrag(E,x,y);
- return true;
- }
-
- public void update(Graphics G) {
- paint(G);
- }
-
-
- public void paint(Graphics G) {
- int g = 0;
-
- G.setColor(Color.red);
- while (All[g].x != -1) {
- G.drawRect(All[g].x,All[g].y,1,1);
- g++;
- }
- }
-
- }
-
-
-
-